import { entryInfoUrl, entrySaveUrl, userInfoUrl, userUpdateeInfoUrl, familyListUrl } from '../../../../api/individual'
import { post, get } from '../../../../utils/index'
import WxValidate from '../../../../utils/WxValidate'
import Dialog from '@vant/weapp/dialog/dialog'

Page({
  data: {
    page: '',
    readonly: false,
    isEdit: false,
    bgList: ['blue-color-bg', 'yellow-color-bg', 'green-color-bg', 'pink-color-bg', 'purple-color-bg'],
    title: '家庭成员',
    info: {},
    family: [],
    form: {},
    formError: {}
  },
  async loadData () { // 获取人员信息
    const url = this.data.page === 'entry' ? entryInfoUrl : userInfoUrl
    let { data, data: { family } } = await post(url)
    if (data.status === 3 || data.status === 4) {
      const { data: { rows } } = await get(familyListUrl, { page: 1, pageSize: 30 })
      rows.forEach(item => {
        item.job = item.position
        item.relationCode = item.relation
        item.company = item.workUnit
        item.isDeleted = 0
      })
      family = rows
    }
    this.setData({ info: data, family })
  },
  save () { // 保存
    const { form, family, info, editIndex } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      wx.showToast({ title: '信息填写有误，请检查', icon: 'none' })
      return
    }
    if (editIndex > -1) {
      family[editIndex] = form
    } else {
      family.push(form)
    }
    info.family = family
    this.submit()
  },
  async submit () { // 提交
    const { page } = this.data
    const url = page === 'entry' ? entrySaveUrl : userUpdateeInfoUrl
    await post(url, this.data.info)
    this.loadData()
    this.setData({ isEdit: false, editIndex: -1, title: '家庭成员' })
  },
  addFamily () { // 添加家庭成员
    const form = {}
    this.setData({ form, isEdit: true, title: '添加家庭成员' })
  },
  async addAgain () { // 继续添加
    const { form, family, info, editIndex, page } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      wx.showToast({ title: '信息填写有误，请检查', icon: 'none' })
      return
    }
    if (editIndex > -1) {
      family[editIndex] = form
    } else {
      family.push(form)
    }
    info.family = family
    const url = page === 'entry' ? entrySaveUrl : userUpdateeInfoUrl
    await post(url, info)
    this.setData({ form: {}, isEdit: true, editIndex: -1, title: '添加家庭成员' })
  },
  editFamily ({ currentTarget }) { // 编辑家庭成员
    if (this.data.readonly) {
      return
    }
    const { index } = currentTarget.dataset
    const form = this.data.family[index]
    this.setData({ form, isEdit: true, title: '编辑家庭成员', editIndex: index })
  },
  delFamily ({ target, detail }) { // 删除家庭成员
    const { instance } = detail
    Dialog.confirm({
      message: '确定删除该家庭成员吗？',
    }).then(() => {
      instance.close()
      const { index } = target.dataset
      if (this.data.info.status === 3 || this.data.info.status === 4) {
        this.data.family[index].isDeleted = 1
        this.data.info.family = this.data.family
      } else {
        this.data.family.splice(index, 1)
      }
      this.submit()
    }).catch(() => {
      instance.close()
      Dialog.close()
    })
  },
  nameChange ({ detail }) { // 成员姓名
    this.setData({ 'form.name': detail })
  },
  relationCodeChange ({ detail }) { // 称谓
    this.setData({ 'form.relationCode': detail })
  },
  companyChange ({ detail }) { // 单位
    this.setData({ 'form.company': detail })
  },
  jobChange ({ detail }) { // 职务
    this.setData({ 'form.job': detail })
  },
  phoneChange ({ detail }) { // 联系电话
    this.setData({ 'form.phone': detail })
  },
  onClickLeft () { // 返回
    if (this.data.isEdit) {
      this.setData({ isEdit: false, title: '家庭成员' })
      this.loadData()
      return
    }
    wx.navigateBack({})
  },
  initValidator () { // 初始化验证器
    const rules = {
      name: { required: true },
      relationCode: { required: true },
      company: { required: true },
      job: { required: true },
      phone: { required: true, tel: true }
    }
    const messages = {
      name: { required: '请输入成员姓名' },
      relationCode: { required: '请输入成员称谓' },
      company: { required: '请输入单位' },
      job: { required: '请输入职务' },
      phone: { required: '请输入联系电话', tel: '请输入正确手机号' }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  readonlyChange () { // 编辑资料
    this.setData({ readonly: false })
  },
  onLoad (options) {
    // page表示从哪个页面过来的, entry->入职； change->修改；me->我的
    const { page } = options
    const readonly = page === 'me'
    this.setData({ readonly, page })
    this.loadData()
    this.initValidator()
  }
})